/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"808EBEBF-FD08-4A38-9DF7-482AF5EEE96D",variableType:4}
 */
var digito = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"FF8FEFB8-B78E-4853-B5B8-34FFD3B1CA8C"}
 */
var f_contacto = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"FB46AE7A-7C84-49DA-948D-0C2CE328FD4F"}
 */
function iniciar(firstShow, event) 
{
	if (globals.gClienteDesde == null)
	{
		controller.newRecord();
		tipo_iva_id = globals.zparam_cond_iva_default;
		f_contacto = null;
		elements.cuit.requestFocus(); //08/11/2011
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"49686E75-48A3-4F42-B29C-FEE68117E7EF"}
 */
function cancelar(event) 
{
	databaseManager.revertEditedRecords()
	var $win = application.getWindow('clientes_alta');
	$win.hide()
	$win.destroy()
}

/**
 * Handle changed data.
 *
 * @param {Object} [oldValue] old value
 * @param {Object} [newValue] new value
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"C7AC360B-CE37-414C-918C-E49711426B04"}
 * @AllowToRunInFind
 */
function buscar_x_cuit(oldValue, newValue, event) 
{
	if (cuit==null)
	{ // cuit no es obligatorio, pero si lo ingresa se valida 
		return false;
	}
	/** @type {JSFoundSet<db:/infocom/clientes>}*/
	var fs_cli = databaseManager.getFoundSet(databaseManager.getDataSourceServerName(controller.getDataSource()),'clientes')
	fs_cli.find();
	fs_cli.cuit	= cuit;
	var found	= fs_cli.search();
	if (found !=0)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Ya existe el CUIT ' + fs_cli.cuit + ' para el cliente ' + fs_cli.razon_social,'OK')
		cuit=null;
		return false;
	}
	if (cuit!=null) // 31/05-01/06/2011
	{

		if(globals.calcula_cuit(cuit)==false)
		{
			globals.DIALOGS.showWarningDialog('Aviso','CUIT erróneo ','OK')
			cuit[12]=null;
			digito=null;
			//elements.cuit.requestFocus();
			return false;
			}
		else
		{
			elements.razon_social.requestFocus();
			return true;
		}
	}
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"29DE0FF2-DADD-4215-83AD-56DC85B0D6F1"}
 */
function confirmar(event) 
{
	bloqueado			= 0;
	credito_asignado	= 0;
	plazo_pago			= 0;
	inactivo			= 0;
	
	if (razon_social==null || tipo_iva_id==null)
	{
		var $msj
		if (razon_social==null) 
			$msj = 'Debe ingresar Razon Social';
		if (tipo_iva_id==null)
			$msj = 'Debe ingresar Tipo Iva';
		globals.DIALOGS.showWarningDialog('Aviso',$msj,'OK')
		return;
	}

	if (buscar_x_cuit() == false)
		return;

	// ALTA CONTACTO 
	if(globals.gClienteDesde==null)
	{
		if (email!=null || f_contacto!=null  || telefono!=null)
		{
			clientes_to_contactos_cl.newRecord();
			clientes_to_contactos_cl.email		= email;
			clientes_to_contactos_cl.nombre		= f_contacto;
			clientes_to_contactos_cl.telefono	= telefono;
		}
	}
	
	if (databaseManager.saveData(foundset) == false)
	{
		globals.DIALOGS.showWarningDialog('Aviso','No pudo grabar cliente ' + razon_social + ' revise los datos cargados','OK')
		return;
	}

	globals.gClienteDesde = cliente_id;  //  devuelve el codigo generado
	var $win = application.getWindow('clientes_alta');
	$win.hide()
	$win.destroy()
}

/**
 * Handle focus element loosing focus.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"4B221009-2D58-4F55-92C0-3A9679DB750E"}
 */
function onFocusLost_cuit(event) 
{
	if (cuit!=null)
	{
		if (globals.calcula_cuit(cuit)==false)
		{
			//elements.cuit.requestFocus();
			return false;
		}
		else
			return true;
	}
	else
		return true;
}
